package com.taobao.android.mnndemo;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.graphics.Rect;
import android.graphics.YuvImage;

import java.io.ByteArrayOutputStream;

public class ImageUtil {

    public static Bitmap convertYuvToBitmap(byte[] yuvData, int width, int height) {
        return convertYuvToBitmap(yuvData, width, height, null);
    }
    public static Bitmap convertYuvToBitmap(byte[] yuvData, int width, int height, BitmapFactory.Options opts) {
        // 创建 YuvImage 对象
        YuvImage yuvImage = new YuvImage(yuvData, ImageFormat.NV21, width, height, null);

        // 创建输出流
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        
        // 将 YuvImage 压缩为 JPEG 格式
        yuvImage.compressToJpeg(new Rect(0, 0, width, height), 100, outputStream);

        // 将输出流转换为 byte[]
        byte[] jpegData = outputStream.toByteArray();
        
        // 将 byte[] 转换为 Bitmap
        return BitmapFactory.decodeByteArray(jpegData, 0, jpegData.length, opts);
    }

    /**
     * 从 byte[] 中解码 Bitmap，并应用缩放选项以减少内存消耗
     *
     * @param YUV 图像数据的 byte 数组
     * @param reqWidth  目标宽度
     * @param reqHeight 目标高度
     * @return 缩放后的 Bitmap
     */
    public static Bitmap decodeSampledBitmapFromByteArray(byte[] YUV, int reqWidth, int reqHeight, int width, int height) {
        // 第一次解码以检查尺寸
        final BitmapFactory.Options options = new BitmapFactory.Options();


        // 创建 YuvImage 对象
        YuvImage yuvImage = new YuvImage(YUV, ImageFormat.NV21, width, height, null);

        // 创建输出流
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

        // 将 YuvImage 压缩为 JPEG 格式
        yuvImage.compressToJpeg(new Rect(0, 0, width, height), 100, outputStream);

        // 将输出流转换为 byte[]
        byte[] jpegData = outputStream.toByteArray();

        BitmapFactory.decodeByteArray(jpegData, 0, jpegData.length, options);

        options.inJustDecodeBounds = true;
        // 计算 inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        // 使用 inSampleSize 解码
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeByteArray(jpegData, 0, jpegData.length, options);
    }

    /**
     * 计算 inSampleSize 用于缩放图像
     *
     * @param options   BitmapFactory.Options 包含原始尺寸
     * @param reqWidth  目标宽度
     * @param reqHeight 目标高度
     * @return inSampleSize 值
     */
    public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
        // 原始图像的宽高
        final int height = options.outHeight;
        final int width = options.outWidth;
        int inSampleSize = 1;

        if (height > reqHeight || width > reqWidth) {
            final int halfHeight = height / 2;
            final int halfWidth = width / 2;

            // 计算 inSampleSize，保证它是 2 的幂，并且使得最终尺寸接近但不小于目标尺寸
            while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) {
                inSampleSize *= 2;
            }
        }
        return inSampleSize;
    }
    public static byte[] getBytesFromBitmap(Bitmap bitmap, Bitmap.CompressFormat format, int quality) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(format, quality, byteArrayOutputStream);
        return byteArrayOutputStream.toByteArray();
    }
}